package com.koron.common.core.util.weixin.bean;

import com.koron.common.core.business.configsettings.utils.ConfigInfoTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @author jzp
 * @since 2021/11/4
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class WxProgramProperties {
    private String appId;
    private String templateId;
    private String secret;
    private String touser;

    public WxProgramProperties() {
        /*Map<String, String> map = ConfigInfoTool.getConfigByCode("common", "wx_miniprogram");
        if (map.isEmpty()) {
            return;
        }
        this.appId = map.get("appId");
        this.templateId = map.get("templateId");
        this.secret = map.get("secretId");
        this.touser = map.get("touser");*/
    }

    public String getAppId() {
        if(StringUtils.isBlank(appId)) {
            Map<String, String> map = ConfigInfoTool.getConfigByCode("common", "wx_miniprogram");
            Assert.notEmpty(map, "[common:wx_miniprogram]未配置");
            this.appId = map.get("appId");
            Assert.hasText(appId, "[common:wx_miniprogram:appId]未配置");
        }
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTemplateId() {
        if(StringUtils.isBlank(templateId)) {
            Map<String, String> map = ConfigInfoTool.getConfigByCode("common", "wx_miniprogram");
            Assert.notEmpty(map, "[common:wx_miniprogram]未配置");
            this.templateId = map.get("templateId");
            Assert.hasText(templateId, "[common:wx_miniprogram:templateId]未配置");
        }
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getSecret() {
//        if(StringUtils.isBlank(secret)) {
            Map<String, String> map = ConfigInfoTool.getConfigByCode("common", "wx_miniprogram");
            Assert.notEmpty(map, "[common:wx_miniprogram]未配置");
            this.secret = map.get("secretId");
            Assert.hasText(secret, "[common:wx_miniprogram:secretId]未配置");
//        }
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getTouser() {
        if(StringUtils.isBlank(touser)) {
            Map<String, String> map = ConfigInfoTool.getConfigByCode("common", "wx_miniprogram");
            Assert.notEmpty(map, "[common:wx_miniprogram]未配置");
            this.touser = map.get("touser");
            Assert.hasText(touser, "[common:wx_miniprogram:touser]未配置");
        }
        return touser;
    }

    public void setTouser(String touser) {
        this.touser = touser;
    }
}
